package com.wwwc.util.web;

import java.util.*;

public class MyHtmlForm {
    
    private static  String month[]={"January","February","March","April","May",
				   "June","July","August","September","October",
				   "November","December"};
    
    public static String cleanInputTextField(String in) {
	if(in == null) {
	    return "";
	}
	if(in.indexOf("\"") !=-1) {
	    return (in.replaceAll("\"","&quot;"));
	}
	return in;
    }

    public static StringBuffer getTextBox (String name, String init, String size, String mxsize) {
	StringBuffer sb = new StringBuffer();
	if(init == null) {
	    init = "";
	}
	sb.append("\n<input type='text' name='"+name+"' value='"+init+"' size='"+size+"' maxLength='"+mxsize+"'>");
	return sb;
    }

    public static StringBuffer getTextBox (String name, String init, int size, int mxsize) {
	StringBuffer sb = new StringBuffer();
	if(init == null) {
	    init = "";
	}
	sb.append("\n<input type='text' name='"+name+"' value='"+init+"' size='"+size+"' maxLength='"+mxsize+"'>");
	return sb;
    }

    public static StringBuffer getTextBox (int sw, String name, String init, int size, int mxsize) {
	StringBuffer sb = new StringBuffer();
	if(init == null) {
	    init = "";
	}
	sb.append("\n<input type='text' name='"+name+"' value='"+init+"' size='"+MyScreen.getWidth(sw, size)+"' maxLength='"+mxsize+"'>");
	return sb;
    }

    public static StringBuffer getTextArea (int sw, String name, String init, int rows, int cols) {
	StringBuffer sb = new StringBuffer();
	if(init == null) {
	    init = "";
	}
	sb.append("<TEXTAREA name='"+name+"' rows='"+rows+"' cols='"+MyScreen.getWidth(sw, cols)+"'>");
	sb.append(init);
	sb.append("</TEXTAREA>");
	return sb;
    }
    
    public static StringBuffer getSelect (String name, Hashtable hs) {
	StringBuffer sb = new StringBuffer();
	sb.append("\n<select name="+name+" size=1>");
	String temp = null;
	Enumeration enums = hs.keys();
	while(enums.hasMoreElements()) {
	    temp = (String)enums.nextElement();
	    sb.append("\n<option value="+hs.get(temp)+">"+temp+"</option>");
	}
	sb.append("\n</select>");
	return sb;
    }

    public static StringBuffer getSelect (String name, ArrayList a, String selected) {
	StringBuffer sb = new StringBuffer();
	sb.append("\n<select name="+name+" size=1  style='font-size:8pt'>");
	sb.append("\n<option>--------</option>");
	String temp = null;
	Iterator it = a.iterator();
	while(it.hasNext()) {
	    temp = (String)it.next();
	    if(temp != null && selected != null && temp.equals(selected)) {
		sb.append("\n<option selected>"+temp+"</option>");
	    }
	    else {
		sb.append("\n<option>"+temp+"</option>");
	    }
	}
	sb.append("\n</select>");
	return sb;
    }

    public static StringBuffer getSelect (String name, Vector v, String selected) {
	StringBuffer sb = new StringBuffer();
	sb.append("\n<select name="+name+" size=1  style='font-size:8pt'>");
	sb.append("\n<option>--------</option>");
	String temp = null;
	Enumeration enums = v.elements();
	while(enums.hasMoreElements()) {
	    temp = (String)enums.nextElement();
	    if(temp != null && selected != null && temp.equals(selected)) {
		sb.append("\n<option selected>"+temp+"</option>");
	    }
	    else {
		sb.append("\n<option>"+temp+"</option>");
	    }
	}
	sb.append("\n</select>");
	return sb;
    }

    public static StringBuffer getSelect (String name, String options, String dim, 
					   boolean multiple, String default_value, String more) {
	StringBuffer sb = new StringBuffer();
	if(multiple) {
	    sb.append("\n<select name='"+name+"' size='1' multiple "+more+">");
	}
	else {
	    sb.append("\n<select name='"+name+"' "+more+">");
	}
	if(default_value == null) {
	    sb.append("\n<option selected>--------</option>");
	}
	else {
	    sb.append("\n<option>--------</option>");
	}
	String temp = null;
	StringTokenizer tokens = new StringTokenizer(options, dim);
	while (tokens.hasMoreTokens()) {
	    temp = tokens.nextToken();
	    if(temp != null && temp.length()>0) {
		if(default_value != null && temp.equals(default_value)) {
		    sb.append("\n<option selected>"+temp+"</option>");
		}
		else {
		    sb.append("\n<option>"+temp+"</option>");
		}
	    }
	}

	sb.append("\n</select>");
	return sb;
    }

    public static StringBuffer getSelect (String name, Vector optionV, boolean multiple, String default_value) {
	StringBuffer sb = new StringBuffer();
	if(multiple) {
	    sb.append("\n<select name='"+name+"' size='1' multiple style='font-size: 8pt'>");
	}
	else {
	    sb.append("\n<select name='"+name+"' size='1' style='font-size: 8pt'>");
	}

	if(default_value == null) {
	    sb.append("\n<option selected>--------</option>");
	}
	else {
	    sb.append("\n<option>--------</option>");
	}

	String temp = null;
	Enumeration enums = optionV.elements();
	while (enums.hasMoreElements()) {
	    temp = (String)enums.nextElement();
	    if(temp != null && temp.length()>0) {
		if(default_value != null && temp.equals(default_value)) {
		    sb.append("\n<option selected>"+temp+"</option>");
		}
		else {
		    sb.append("\n<option>"+temp+"</option>");
		}
	    }
	}

	sb.append("\n</select>");
	return sb;
    }

    public static StringBuffer getOptions(int from, int to, String default_value) 
    {
	if(default_value == null) {
	    return getOptions(from, to, from);
	}
	else {
	    return getOptions(from, to, Integer.parseInt(default_value));
	}
    }

    public static StringBuffer getOptions(int from, int to, int default_value) 
    {
	StringBuffer sb = new StringBuffer();
	
	for(int i= from; i <= to; i++) {
	    if(i==default_value) {
		sb.append("<option selected>"+i+"</option>");
	    }
	    else {
		sb.append("<option>"+i+"</option>");
	    }
	}
	
	return sb;
    }

    public static StringBuffer getSelectYear (int m, int n) {
	Calendar c = Calendar.getInstance();
	int y = c.get(Calendar.YEAR);

	StringBuffer sb = new StringBuffer();
	for(int i= m; i <= n; i++) {
	    c.set((y-i),0, 1, 0, 0);
	    sb.append("\n<option value="+c.get(Calendar.YEAR)+" O"+c.get(Calendar.YEAR)+
		      ">"+c.get(Calendar.YEAR)+"</option>");
	}

	return sb;
    }

    public static StringBuffer getSelectBirthYear (String selected) {
	int n = -1;
	if(selected != null && selected.length()>0){
	    if((new MyUserInputCheck()).checkNumber(selected)) {
		n = Integer.parseInt(selected);
	    }
	}

	Calendar c = Calendar.getInstance();
	int y = c.get(Calendar.YEAR);

	StringBuffer sb = new StringBuffer();
	for(int i= 1; i <= 120; i++) {
	    c.set((y-i),0, 1, 0, 0);
	    if(n == c.get(Calendar.YEAR)) {
		sb.append("\n<option value="+c.get(Calendar.YEAR)+" selected>"+c.get(Calendar.YEAR)+"</option>");
	    }
	    else {
		sb.append("\n<option value="+c.get(Calendar.YEAR)+">"+c.get(Calendar.YEAR)+"</option>");
	    }
	}

	return sb;
    }

    public static StringBuffer getSelectBirthMonth (String selected) {
	int n = -1;
	if(selected != null && selected.length()>0){
	    if((new MyUserInputCheck()).checkNumber(selected)) {
		n = Integer.parseInt(selected);
	    }
	}

	StringBuffer sb = new StringBuffer();
	for(int i= 0; i <= 11; i++) {
	    if(n == i+1) {
		sb.append("\n<option value="+(i+1)+" selected>"+month[i]+"</option>");
	    }
	    else {
		sb.append("\n<option value="+(i+1)+">"+month[i]+"</option>");
	    }
	}
	return sb;
    }
    
    public static StringBuffer getSelectBirthDay (String selected) {
	int n = -1;
	if(selected != null && selected.length()>0){
	    if((new MyUserInputCheck()).checkNumber(selected)) {
		n = Integer.parseInt(selected);
	    }
	}

	StringBuffer sb = new StringBuffer();

	for(int i= 1; i <= 31; i++) {
	    if(n == i) {
		sb.append("\n<option value="+i+" selected>"+i+"</option>");
	    }
	    else {
		sb.append("\n<option value="+i+">"+i+"</option>");
	    }
	}
	return sb;
    }

    public static StringBuffer getSelectBirthYear (int age) {
	Calendar c = Calendar.getInstance();
	int y = c.get(Calendar.YEAR);

	StringBuffer sb = new StringBuffer();
	for(int i= age; i <= 120; i++) {
	    c.set((y-i),0, 1, 0, 0);
	    sb.append("\n<option value="+c.get(Calendar.YEAR)+" O"+c.get(Calendar.YEAR)+
		      ">"+c.get(Calendar.YEAR)+"</option>");
	}

	return sb;
    }
    /*
      <option value="872">Accounting, Auditing & Bookkeeping
      <option value="731">Advertising
      <option value="020">Agricultural Production - Livestock
      <option value="070">Agricultural Services
      <option value="230">Apparel and Other Textile Products
      <option value="553">Auto and Home Supply Stores
      <option value="559">Automotive Dealers, NEC
      <option value="751">Automotive Rentals, No Drivers
      <option value="753">Automotive Repair Shops
      <option value="754">Automotive Services, Except Repair
      <option value="724">Barber Shops
      <option value="723">Beauty Shops
      <option value="555">Boat Dealers
      <option value="793">Bowling Centers
      <option value="703">Camps and Recreational Vehicle Parks
      <option value="544">Candy, Nut, and Confectionery Stores
      <option value="175">Carpentry and Floor Work
      <option value="280">Chemicals and Allied Products
      <option value="835">Child Day Care Services
      <option value="564">Children's and Infants' Wear Stores
      <option value="480">Communications
      <option value="737">Computer and Data Processing Services
      <option value="177">Concrete Work
      <option value="353">Construction and Related Machinery
      <option value="170">Construction Special Trade Contractors
      <option value="732">Credit Reporting and Collection
      <option value="531">Department Stores
      <option value="600">Depository Institutions
      <option value="591">Drug Stores and Proprietary Stores
      <option value="581">Eating and Drinking Places
      <option value="820">Educational Services
      <option value="490">Electric, Gas, and Sanitary Services
      <option value="762">Electrical Repair Shops
      <option value="173">Electrical Work
      <option value="360">Electronic & Other Electronic Equipment
      <option value="871">Engineering & Architectural Services
      <option value="340">Fabricated Metal Products
      <option value="565">Family Clothing Stores
      <option value="352">Farm and Garden Machinery
      <option value="200">Food and Kindred Products
      <option value="080">Forestry
      <option value="598">Fuel Dealers
      <option value="726">Funeral Service and Crematories
      <option value="250">Furniture and Fixtures
      <option value="571">Furniture and Homefurnishings Stores
      <option value="554">Gasoline Service Stations
      <option value="150">General Building Contractors
      <option value="356">General Industry Machinery
      <option value="541">Grocery Stores
      <option value="799">Gyms, Fitness, Recreation & Amusement
      <option value="525">Hardware Stores
      <option value="809">Health and Allied Services, NEC
      <option value="160">Heavy Construction, Except Building
      <option value="018">Horticultural Specialties
      <option value="806">Hospitals
      <option value="701">Hotels and Motels
      <option value="572">Household Appliance Stores
      <option value="832">Individual and Family Services
      <option value="359">Industrial Machinery, NEC
      <option value="380">Instruments and Related Products
      <option value="640">Insurance Agents, Brokers & Service
      <option value="078">Landscape and Horticultural Services
      <option value="721">Laundry, Cleaning, & Garment Services
      <option value="811">Legal Services
      <option value="592">Liquor Stores
      <option value="410">Local and Interurban Passenger Transit
      <option value="521">Lumber and Other Building Materials
      <option value="240">Lumber and Wood Products
      <option value="733">Mailing, Reproduction, Stenographic
      <option value="874">Management & Public Relations
      <option value="174">Masonry, Stonework, and Plastering
      <option value="542">Meat and Fish Markets
      <option value="561">Men's & Boys' Clothing Stores
      <option value="354">Metalworking Machinery
      <option value="569">Misc. Apparel & Accessory Stores
      <option value="735">Misc. Equipment Rental & Leasing
      <option value="539">Misc. General Merchandise Stores
      <option value="179">Misc. Special Trade Contractors
      <option value="738">Miscellaneous Business Services
      <option value="549">Miscellaneous Food Stores
      <option value="390">Miscellaneous Manufacturing Industries
      <option value="729">Miscellaneous Personal Services
      <option value="769">Miscellaneous Repair Shops
      <option value="594">Miscellaneous Shopping Goods Stores
      <option value="527">Mobile Home Dealers
      <option value="780">Motion Pictures
      <option value="557">Motorcycle Dealers
      <option value="610">Nondepository Institutions
      <option value="140">Nonmetallic Minerals, Except Fuels
      <option value="596">Nonstore Retailers
      <option value="805">Nursing and Personal Care Facilities
      <option value="801">Offices & Clinics of Medical Doctors
      <option value="802">Offices and Clinics of Dentists
      <option value="804">Offices of Other Health Practitioners
      <option value="130">Oil and Gas Extraction
      <option value="523">Paint, Glass, and Wallpaper Stores
      <option value="736">Personnel Supply Services
      <option value="290">Petroleum and Coal Products
      <option value="722">Photographic Studios, Portrait
      <option value="171">Plumbing, Heating, Air-Conditioning
      <option value="330">Primary Metal Industries
      <option value="270">Printing and Publishing
      <option value="792">Producers, Orchestras, Entertainers
      <option value="573">Radio, Television, & Computer Stores
      <option value="650">Real Estate
      <option value="556">Recreational Vehicle Dealers
      <option value="546">Retail Bakeries
      <option value="526">Retail Nurseries and Garden Stores
      <option value="599">Retail Stores, NEC
      <option value="764">Reupholstery and Furniture Repair
      <option value="176">Roofing, Siding, and Sheet Metal Work
      <option value="300">Rubber and Misc. Plastics Products
      <option value="620">Security and Commodity Brokers
      <option value="734">Services to Buildings
      <option value="725">Shoe Repair and Shoeshine Parlors
      <option value="566">Shoe Stores
      <option value="355">Special Industry Machinery
      <option value="320">Stone, Clay, and Glass Products
      <option value="220">Textile Mill Products
      <option value="450">Transportation by Air
      <option value="370">Transportation Equipment
      <option value="470">Transportation Services
      <option value="420">Trucking and Warehousing
      <option value="593">Used Merchandise Stores
      <option value="552">User Car Dealers
      <option value="533">Variety Stores
      <option value="074">Veterinary Services
      <option value="784">Video Tape Rental
      <option value="763">Watch, Clock, and Jewelry Repair
      <option value="440">Water Transportation
      <option value="178">Water Well Drilling
      <option value="500">Wholesale Trade - Durable Goods
      <option value="510">Wholesale Trade - Nondurable Goods
      <option value="563">Women's Accessory & Specialty Stores
      <option value="562">Women's Clothing Stores
    */
}



